/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.UserFacade;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Named;
import javax.inject.Inject;

/**
 * This class is used to interact with the file userRight.xhtml
 * It provides methods to render the web page and call higher method in the controller
 * @author Guillaume, Yann
 */
@Named(value = "changeUserRight")
@ConversationScoped
public class ChangeUserRight implements Serializable{
    @Inject
    private Conversation conversation;
    private String name;
    private boolean ban ;
    private boolean banChange = false;
    private String grade;
    
    private Exception transactionFailure;
    @EJB
    private UserFacade userFacade;
    
    /**
     * Start a conversation to store variable relative to the session
     */
    private void startConversation() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }
    
    /**
     * Stop the current conversation, used to log out the user
     */
    private void stopConversation() {
        if (!conversation.isTransient()) {
            conversation.end();
        }
    }
    
    /**
     * Handle the exception and set the transaction failure
     * @param e catch the exception coming from somewhere else
     */
    private void handleException(Exception e) {
        stopConversation();
        e.printStackTrace(System.err);
        transactionFailure = e;
    }
    
    /**
     * Returns the latest thrown exception.
     */
    public Exception getException() {
        return transactionFailure;
    }
    
    /**
     * Returns <code>false</code> if the latest transaction failed, otherwise <code>false</code>.
     */
    public boolean getSuccess() {
        return transactionFailure == null;
    }
    
    /**
     * Check if a user is ban
     * @return boolean, true if user is ban
     */
    public boolean isBan() {
        return ban;
    }
    
    /**
     * Change the ban attribute of this manager
     * @param ban new ban attribute for the manager
     */
    public void setBan(boolean ban) {
        this.ban = ban;
    }
    
    /**
     * Return the grade of the current user
     * @return A string within the grade is store
     */
    public String getGrade() {
        return grade;
    }
    
    /**
     * Change grade of the user that is looking the page
     * @param grade new grade
     */
    public void setGrade(String grade) {
        this.grade = grade;
    }

    /**
     * Name of the user
     * @return  name of the user
     */
    public String getName() {
        return name;
    }
    
    /**
     * Change the user name
     * @param name new user name
     */
    public void setName(String name) {
        this.name = name;
    }

    /** Creates a new instance of ChangeUserRight */
    public ChangeUserRight() {
    }
    /**
     * Change the right of a client, call a higher method
     * @param clientName name of the client which right will be changed
     * @param clientGrade new grade fr the client
     * @param clientBan  new ban attribute for the client
     */
    public void changeRight(String clientName, String clientGrade, Boolean clientBan){
        try {
            boolean ban;
            String grade;
            if ( this.banChange != true ){
                ban = clientBan;
            } else{
                ban = this.ban;
            }
            if( this.grade == null){
                grade = clientGrade;
            } else{
                grade = this.grade;
            }
            System.out.println("Submit ask");
            int gr = Integer.parseInt(grade);
            userFacade.changeRight(clientName, gr, ban);
            this.banChange = false;
            this.grade = null;
        } catch (Exception ex) {
            handleException(ex);
        }
    }
    
    /**
     * Use to change the ban attribute for view manager
     * @param vce event coming from the view, when user change proprety of the checkbox ban
     */
    public void changeBan(ValueChangeEvent vce){
        Boolean str = (Boolean) vce.getNewValue();
        if (str){
            //System.out.println("IN");
            this.ban = true;
        }else if(!str){
            //System.out.println("OUT");
            this.ban = false;
        }
        //this.banChange = true;
        System.out.println("VCE val: "+vce.getNewValue());
    }
    
    /**
     * Change the grade of the view manager
     * @param vce event coming from the view, when user change proprety of grade in the view
     */
    public void changeGrade(ValueChangeEvent vce){
        int gr =  (Integer) vce.getNewValue();
        this.grade = String.valueOf(gr);
        System.out.println("Grade change: "+gr);
    }
}
